/**
 * FileServiceRegistryTestdriver.java
 */
package fss;

import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import fss.common.communication.QueryResult;
import fss.remote.ServiceRegistry;
import fss.remote.util.RemoteFileServiceRegistry;
import fss.util.rmi.RmiId;
import fss.util.rmi.RmiUtils;

/**
 * Test each operation of a remote file service registry.
 * 
 * @author thinh
 *
 */
public class FileServiceRegistryTestdriver {
    private static final int QUIT = 3;
    /**
     * Display menu and get user selection.
     * @return selection.
     */
    private static int getMenuOption() {
        int choice = -1;
        Scanner stdin = new Scanner(System.in);
        System.out.println("-- Remote File Service Registry Menu -- ");
        System.out.println("1. getRegisteredServiceList: Get all registered service(s)");
        System.out.println("2. query: Query for a file");
        System.out.println("3. Quit");
        System.out.print("> ");
        String input = stdin.next();
        try {
            choice = Integer.parseInt(input);
        } catch (Exception e) {
            // ignore
        }
        return choice;
    }
    /**
     * Build an RMI identifier.
     * @param title a title for the inputs.
     * @return a rmi identifier.
     */
    private static RmiId getRmiId(String title) {
        Scanner stdin = new Scanner(System.in);
        RmiId id = null;
        String host, name;
        int port;
        System.out.println(title);
        System.out.print("Enter remote host: ");
        host = stdin.nextLine();
        System.out.print("Enter remote port: ");
        port = Integer.parseInt(stdin.nextLine());
        System.out.print("Enter remote object name: ");
        name = stdin.nextLine();
        id = new RmiId(host, name, port);
        return id;
    }
    /**
     * List all registered service(s)
     * @throws RemoteException error invoking on remote registry.
     */
    public void doOption1() throws RemoteException {
        RmiId registryId = getRmiId("-- Remote Registry Identifier --");
        ServiceRegistry registry = (ServiceRegistry)RmiUtils.getRemoteObject(registryId);
        System.out.println();
        
        Set<RmiId> registration = registry.getRegisteredServiceList();
        System.out.println("Registered service(s) on " + registryId);
        int count = 1;
        for(Iterator<RmiId> itr = registration.iterator(); itr.hasNext(); count++) {
            RmiId info = itr.next();
            System.out.println(count + ". " + info);
        }
        System.out.println();
    }
    /**
     * Query for a file. 
     * @throws RemoteException error invoking on remote registry.
     */
    public void doOption2() throws RemoteException {
        Scanner stdin = new Scanner(System.in);
        RmiId serviceId = getRmiId("-- Remote Registry Identifier --");
        RemoteFileServiceRegistry registry = (RemoteFileServiceRegistry)RmiUtils.getRemoteObject(serviceId);
        
        String param = null;
        System.out.print("Enter a search string: ");
        param = stdin.nextLine();
        List<QueryResult> results = registry.query(param);
        System.out.println("Query results");
        int count = 1;
        for(QueryResult r : results) {
            System.out.println((count++) + ". " + r);
        }
        System.out.println();
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("******************************************************************");      
        System.out.println("Remote File Service Registry Testdriver (dev-only)");
        System.out.println("******************************************************************");
        System.out.println();
        
        FileServiceRegistryTestdriver driver = new FileServiceRegistryTestdriver();
        int choice = -1;
        choice = getMenuOption();
        do {
            switch(choice) {
                case 1: // list all registered service(s)
                    try {
                        driver.doOption1();
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    try {
                        driver.doOption2();
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    System.out.println("Unknown option " + choice + ". Please try again.");
                    break;
            }
            choice = getMenuOption();
        } while(choice != QUIT);
        System.out.println("Testdriver terminated.");
    }

}
